#include "tableprintsettings.h"
#include "ui_tableprintsettings.h"
#include "tableprintwidget.h"
#include "ui_tableprintwidget.h"

tablePrintSettings::tablePrintSettings(tablePrintWidget *parent) :
    QDialog(parent),
    ui(new Ui::tablePrintSettings),
    backEngine(parent)
{
    ui->setupUi(this);
    setWindowTitle("");

    ui->receipt_head->setStyleSheet("background:" + tablePrintWidget::colorMap.value("receipt_head").value<QColor>().name());
    ui->sale_head->setStyleSheet("background:" + tablePrintWidget::colorMap.value("sale_head").value<QColor>().name());
    ui->pay_detail->setStyleSheet("background:" + tablePrintWidget::colorMap.value("pay_detail").value<QColor>().name());
    ui->receipt_tail->setStyleSheet("background:" + tablePrintWidget::colorMap.value("receipt_tail").value<QColor>().name());

    //receipt_head
    recordMap("receipt_head", "logo", ui->bp_fmt_title_logo_en);//->setText("\346\211\223\345\215\260Logo", Q_NULLPTR));
//    recordMap("receipt_head", "first_title", ui->bp_fmt_title_enable);//->setText("\347\254\254\344\270\200\350\241\250\345\244\264", Q_NULLPTR));
    recordMap("receipt_head", "second_title", ui->bp_fmt_title_sc_enable);//->setText("\347\254\254\344\272\214\350\241\250\345\244\264", Q_NULLPTR));
    recordMap("receipt_head", "tax_number", ui->bp_fmt_end_tax_number_en);//->setText("Tax No.", Q_NULLPTR));
    recordMap("receipt_head", "top", ui->bp_fmt_top_enable);//->setText("\347\245\250\345\244\264\350\257\255", Q_NULLPTR));

    //sale_head
    recordMap("sale_head", "payno", ui->bp_fmt_payno);//->setText("\344\273\230\346\254\276\345\217\267", Q_NULLPTR));
    recordMap("sale_head", "operid", ui->bp_fmt_operid);//->setText("\345\267\245\345\217\267", Q_NULLPTR));
    recordMap("sale_head", "table", ui->bp_fmt_table);//->setText("\351\244\220\346\241\214", Q_NULLPTR));
    recordMap("sale_head", "person", ui->bp_fmt_person);//->setText("\344\272\272\346\225\260", Q_NULLPTR));
    recordMap("sale_head", "time", ui->bp_fmt_time);//->setText("\346\227\266\351\227\264", Q_NULLPTR));
    recordMap("sale_head", "serial_no", ui->bp_fmt_serial_no);//->setText("\346\265\201\346\260\264\345\217\267", Q_NULLPTR));

    //pay_detail
    recordMap("pay_detail", "num_cost", ui->bp_fmt_cost);//->setText("\346\266\210\350\264\271", Q_NULLPTR));
    recordMap("pay_detail", "num_present", ui->bp_fmt_present);//->setText("\350\265\240\351\200\201", Q_NULLPTR));
    recordMap("pay_detail", "num_discount", ui->bp_fmt_discount);//->setText("\346\212\230\346\211\243", Q_NULLPTR));
    recordMap("pay_detail", "num_rate", ui->bp_fmt_tax);//->setText("\347\250\216\346\224\266", Q_NULLPTR));
    recordMap("pay_detail", "num_free", ui->bp_fmt_free);//->setText("\344\274\230\346\203\240", Q_NULLPTR));

    recordMap("pay_detail", "num_blotout", ui->bp_fmt_blotout);//->setText("\346\212\271\351\233\266", Q_NULLPTR));
    recordMap("pay_detail", "num_realamount", ui->bp_fmt_receive);//->setText("\345\272\224\346\224\266", Q_NULLPTR));
    recordMap("pay_detail", "num_amount", ui->bp_fmt_cash);//->setText("\345\256\236\346\224\266", Q_NULLPTR));
    recordMap("pay_detail", "num_change", ui->bp_fmt_change);//->setText("\346\211\276\351\233\266", Q_NULLPTR));

    //receipt_tail
    recordMap("receipt_tail", "signature", ui->bp_fmt_underwrite);//->setText("\347\255\276\345\220\215", Q_NULLPTR));
    recordMap("receipt_tail", "qr_code", ui->bp_fmt_end_rc_en);//->setText("\346\211\223\345\215\260\344\272\214\347\273\264\347\240\201", Q_NULLPTR));
    recordMap("receipt_tail", "bottom", ui->receipt_end_desc);//->setText("\347\245\250\345\260\276\350\257\255", Q_NULLPTR));

    foreach(QObject *obj, this->findChildren<QCheckBox *>()) {
        connect(obj, SIGNAL(clicked(bool)), this, SLOT(tocheck(bool)));
    }

    //
    for(int r = 0; r < backEngine->ui->tableWidget->rowCount(); r ++) {
        QString key = backEngine->ui->tableWidget->data(r, 0, tablePrintWidget::_u_pre_key).toString();
        QString table = backEngine->ui->tableWidget->data(r, 0, tablePrintWidget::_u_pre_table).toString();

        this->check(key);
    }
    this->updatePixmap(backEngine->printerPixmap(true));
    connect(this, SIGNAL(signal_checked(QStringList, QString, QString, bool)), backEngine, SLOT(toCheckedItem(QStringList, QString, QString, bool)));
    connect(backEngine, SIGNAL(imgRefresh(QPixmap)), this, SLOT(updatePixmap(QPixmap)));
}

tablePrintSettings::~tablePrintSettings()
{
    delete ui;
}

void tablePrintSettings::check(QString key)
{
    if(keyWidgetList.contains(key)) {
        QCheckBox *c = qobject_cast<QCheckBox *>(keyWidgetList.value(key));
        if(c)
            c->setChecked(true);
    }
}

void tablePrintSettings::tocheck(bool checked)
{
    QString key = keyWidgetList.key(sender());
    QString table = keyTableList.value(key);
    emit signal_checked(keyTableList.keys(table), table, key, checked);
}

void tablePrintSettings::updatePixmap(QPixmap pixmap)
{
    pixmap.save("77.png");
    ui->label->setPixmap(pixmap);
}

void tablePrintSettings::recordMap(const QString &table, const QString &key, QObject *widget)
{
    keyWidgetList.append(key, widget);
    keyTableList.append(key, table);
}
void tablePrintSettings::on_pushButton_clicked()
{
    backEngine->on_pushButton_printer_clicked();
}

void tablePrintSettings::on_pushButton_default_clicked()
{
    backEngine->on_pushButton_reset_clicked();
    //
    foreach(QObject *obj, this->findChildren<QCheckBox *>()) {
        qobject_cast<QCheckBox *>(obj)->setChecked(false);
    }
    for(int r = 0; r < backEngine->ui->tableWidget->rowCount(); r ++) {
        QString key = backEngine->ui->tableWidget->data(r, 0, tablePrintWidget::_u_pre_key).toString();
        QString table = backEngine->ui->tableWidget->data(r, 0, tablePrintWidget::_u_pre_table).toString();

        this->check(key);
    }
    updatePixmap(backEngine->printerPixmap(true));
}
